/*

	window.URL = window.URL || window.webkitURL;
	
	var fileElem = document.getElementById("fileElem"),

	Img= document.getElementById("Img");

	var files;
	function handleFiles(obj) {
		files = obj.files,
		img = new Image();
		
		if(window.URL){
			img.src = window.URL.createObjectURL(files[0]); 

			img.onload = function(e) {
				Img.src=this.src;
			}
		}
	}

*/
$(function(){
	$('#Img').on('click',function () {
		$('#modal-form').show();
	})
	
})




var imageFile, x=0,y=0,w=500,h=500;
$('#file_upload').uploadify({
	'buttonText' : '上传图片',
	'swf'      : '/static/uploadify/uploadify.swf',
	'uploader' : '/ajax/fileUpload?type=3&id=1',
	'fileSizeLimit':1*1024*1024,
	'fileObjName' : 'file',
	'fileTypeExts'  : '*.jpg;*.jpge;*.gif;*.png',
	'removeCompleted'	: false,
	'width'	: '100%',
	'buttonClass'	: "btn btn-info",
	'onUploadSuccess' : function(file, data, response) {
		console.log(data);
		alert("1 :"+data)
		$('#avatar2').show();
		$('#avatar2').attr("src", data);
		imageFile=data;
		jcrop_api.setImage(data);
	} ,
	'onInit': function () {
		$("#file_upload-queue").hide();
	}
});
$('#mypic').Jcrop({
	onChange:   setCoords,
	onSelect:   setCoords,
	aspectRatio: xsize / ysize,
	boxWidth: 360,
	boxHeight: 180,
	minSize: [ 36, 20 ],
	maxSize: [ 3600, 2000 ]
},function(){
	jcrop_api = this;
});
function cutPic() {
	w=$('#avatar2').width();
	h=$('#avatar2').height()
	$.ajax({
		type:'get',
		url:'/ajax/newPicCut',
		data:{'x':x,'y':y,'w':w,'h':h,'imageFile':imageFile},
		//data:'imageFile='+ imageFile + '&x=' + x + '&y=' + y + '&w=' + w + '&h=' + h,
		success:function(data) {
			if(data != 'error') {
				if(document.getElementById('images')){
					$('#imageList').find("img").attr("src",data);
					document.getElementById('images').value=data;
					alert("2 :"+data)
				}else{
					alert("3 :"+data)
					$('#imageList').append('<li> \
							<img width="360" height="180" alt="360x180" src="' + data + '" /> \
								<input type="hidden" name="images" value="'+ data+'"/> \
								<div class="tools tools-bottom"> \
									<a href="javascript:void(0);" onclick="del_image(this)"> \
										<i class="ace-icon fa fa-times red"></i> \
									</a> \
								</div> \
							</li>');
				}
			}else {
				alert('图片格式错误,请转换相应格式！');
			}

			if($("#imageList li").length > 1) {
				$('#upPicButton').hide();
			}else {
				$('#upPicButton').show();
			}

			jcrop_api.destroy();
			$('#mypic').Jcrop({
				onChange:   setCoords,
				onSelect:   setCoords,
				aspectRatio: xsize / ysize,
				boxWidth: 360,
				boxHeight: 180,
				minSize: [ 36, 20 ],
				maxSize: [ 3600, 2000 ]
			},function(){
				jcrop_api = this;
			});

			$('#avatar2').hide();
			$('#modal-form').modal('hide');
		}
	});
}

function setCoords(c)
{
	var bounds = jcrop_api.getBounds();
	boundx = bounds[0];
	boundy = bounds[1];

	x=c.x;
	y=c.y;
	w=c.w;
	h=c.h;
	updatePreview(c);
};

function updatePreview(c)
{
	if (parseInt(c.w) > 0)
	{
		var rx = xsize / c.w;
		var ry = ysize / c.h;

		$pimg.css({
			width: Math.round(rx * boundx) + 'px',
			height: Math.round(ry * boundy) + 'px',
			marginLeft: '-' + Math.round(rx * c.x) + 'px',
			marginTop: '-' + Math.round(ry * c.y) + 'px'
		});
	}
};

function del_image(obj) {
	$(obj).parent().parent().remove();
	$('#upPicButton').show();
}